package com.beijiteshop.shop.ui.order.vm

import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.*
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.shop.model.api.request.AddCartOrderRequest
import com.beijiteshop.shop.model.api.request.CartOrderInfoRequest
import com.beijiteshop.shop.model.api.request.OrderRequest
import com.beijiteshop.shop.model.api.request.RefundRequest
import com.beijiteshop.shop.model.api.response.*
import com.beijiteshop.shop.ui.order.repo.OrderRepo
import com.blankj.utilcode.util.LogUtils

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/9 14:22
 * @Description
 */
class OrderVM(private val repo: OrderRepo) : BaseViewModel() {

    val liveOrderConfirm =  MutableLiveData<OrderConfirmBean>()
    val liveCommitOrder = MutableLiveData<String>()
    val liveOrderList = MutableLiveData<List<OrderListBean>>()
    val liveOrderDetail = MutableLiveData<OrderDetailBean>()
    val liveOrderPayInfo = MutableLiveData<OrderPayBean>()
    val liveRefund = MutableLiveData<Pair<Boolean,String?>>()
    val liveCartOrderInfo = MutableLiveData<CartOrderConfirmBean>()
    val liveCommitOrderFailed = MutableLiveData<String>()

    /**
     * 获取订单确认信息
     */
    fun getOrderConfirmInfo(goodId: String) = launch {
        repo.getOrderInfo(goodId)
            .serverData()
            .onSuccess {
                onBizOK { _, data, _ ->
                    liveOrderConfirm.postValue(data)
                }

                onBizError { _, message ->
                    liveError.postValue(message)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun commitOrder(request: OrderRequest) = launch {
        repo.commitOrder(request)
            .serverData()
            .onSuccess {

                onBizOK { code, data, message ->
                    liveCommitOrder.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveError.postValue(it)
                }

            }.onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun getOderList(page: Int,status: Int? = null) = launch {
        repo.getOrderList(page, status)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    if (data.isNullOrEmpty()){
                        liveEmpty.postValue(null)
                    }else{
                        liveOrderList.postValue(data)
                    }
                }
                onBizError { code, message ->
                    liveError.postValue(message)
                }
                onBizEmpty {
                    liveEmpty.postValue(it)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun getOrderDetailInfo(orderId: String) = launch {
        repo.getOrderDetailInfo(orderId)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    liveOrderDetail.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun getOrderPayInfo(orderNum: String) = launch {
        repo.getOrderPayInfo(orderNum)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    data?.let {
                        liveOrderPayInfo.postValue(it)
                    }
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun requestRefund(orderNumber: String,reason: String,money: Float) = launch {
        repo.requestRefund(RefundRequest(orderNumber, reason, money))
            .serverData()
            .onSuccess {
                onBizOk { _, _ ->
                    liveRefund.postValue(Pair(true,null))
                }

                onBizError { _, msg ->
                    liveRefund.postValue(Pair(false,msg))
                }
            }
            .onFailure {
                liveRefund.postValue(Pair(true,it.message))
            }
    }

    /**
     * 获取购物车订单详情数据
     */
    fun getCartOrderInfo(busShopCartIds: Array<String>) = launch {
        repo.getCartOrderInfo(CartOrderInfoRequest(busShopCartIds,""))
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    liveCartOrderInfo.postValue(data)
                }

                onBizError { code, message ->
                    liveError.postValue(message)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    /**
     * 添加购物车订单
     */
    fun addCartOrder(request: AddCartOrderRequest) = launch {
        repo.addCartOrder(request)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    liveCommitOrder.postValue(data)
                }

                onBizError { code, message ->
                    liveCommitOrderFailed.postValue(message)
                }
            }
            .onFailure {
                liveCommitOrderFailed.postValue(it.message)
            }
    }
}